package model;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Entity
@NamedQueries({
        @NamedQuery(
                name = "Address.getAll",
                query = "SELECT a FROM Address a"
        ),
        @NamedQuery(
                name = "Address.getByStreet",
                query = "SELECT a FROM Address a WHERE a.street = :street"
        ),
        @NamedQuery(
                name = "Address.getAddress",
                query = "SELECT a FROM Address a WHERE (a.city = :city) " +
                        "AND (a.street = :street)" +
                        "AND (a.building = :building)" +
                        "AND (a.apartment = :apartment)"
        )
})
public class Address {
    private long id;
    private String city;
    private String street;
    private int building;
    private int apartment;
    private List<Person> persons = new ArrayList<Person>();

    public Address() {
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public int getBuilding() {
        return building;
    }

    public void setBuilding(int building) {
        this.building = building;
    }

    public int getApartment() {
        return apartment;
    }

    public void setApartment(int apartment) {
        this.apartment = apartment;
    }

    @ManyToMany
    public List<Person> getPersons() {
        return persons;
    }

    public void setPersons(List<Person> persons) {
        this.persons = persons;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Address)) return false;

        Address address = (Address) o;

        if (apartment != address.apartment) return false;
        if (building != address.building) return false;
        if (!city.equals(address.city)) return false;
        if (!street.equals(address.street)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = city.hashCode();
        result = 31 * result + street.hashCode();
        result = 31 * result + building;
        result = 31 * result + apartment;
        return result;
    }
}
